﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml.Linq;

namespace SilverMap.Editor.Models
{
	public abstract class ModelBase : INotifyPropertyChanged
	{
		#region INotifyPropertyChanged Members

		public event PropertyChangedEventHandler PropertyChanged = delegate { };

		protected virtual void OnPropertyChanged(string propertyName)
		{
			this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}
		#endregion

		protected abstract void Load(Stream stream);
		protected abstract void Save(Stream stream);

		public void Load(string fileName)
		{
			using (var store = IsolatedStorageFile.GetUserStoreForApplication())
			{
				if (store.FileExists(fileName))
				{
					using (var fs = store.OpenFile(fileName, FileMode.Open))
					{
						Load(fs);
					}
				}
			}
		}

		public void Save(string fileName)
		{
			using (var store = IsolatedStorageFile.GetUserStoreForApplication())
			{
				using (var fs = store.OpenFile(fileName, FileMode.Create, FileAccess.Write))
				{
					Save(fs);
				}
			}
		}
	}
}
